Skip to content

fix(controller): correlate model results by call ID - #299

Open
Tatsat (Tats) Mishra 🐉 (Tatsinnit) wants to merge 3 commits into
agent-primitives:mainfrom
Tatsinnit:fix/93-model-result-correlation
Open

Tatsat (Tats) Mishra 🐉 (Tatsinnit) wants to merge 3 commits into
agent-primitives:mainfrom
Tatsinnit:fix/93-model-result-correlation

Conversation

@Tatsinnit

Copy link
Copy Markdown
Contributor

Summary

  • correlate durable provider-result OUTPUT records with their MODEL_CALL IDs
  • keep direct harness output uncorrelated so handled provider fallback is never replayed as model success
  • preserve pre-v11 positional journals behind an explicit per-call compatibility mode and stamp SQLite schema v11

Fixes #93.

Problem

MODEL_CALL already had an ID, but its durable completion was an ordinary OUTPUT correlated only by position. Direct EventSink.Output produced the same shape.

An in-process harness could therefore handle a provider error, emit fallback output, and crash before END. Replay or Resume would consume that fallback as the successful model response and enter a control-flow branch the original execution never took.

Durable format

New journaled calls carry:

MODEL_CALL { id: C, result_correlation: BY_ID }
OUTPUT     { model_call_id: C, message: ... }

Direct harness output leaves Event.model_call_id absent. The field is proto-optional so absent and explicitly empty remain distinguishable; present-empty is malformed evidence.

ModelCall.result_correlation is the compatibility discriminator. Markerless calls retain legacy positional behavior, because old calls already had nonempty IDs and absence of an output ID alone cannot distinguish an old completion from new direct output.

Replay and recovery

A shared matcher enforces:

  • BY_ID calls consume only an immediately adjacent OUTPUT with the exact nonempty ID
  • absent output ID after BY_ID is direct output and remains unconsumed
  • wrong, empty, or unknown correlation evidence is sticky ErrReplayDiverged
  • direct Output cannot consume a correlated model result
  • a trailing unanswered call is re-driven once and its completion reuses the original durable ID
  • markerless calls accept an old ID-less positional completion or an exact upgraded result

A failed re-drive of a pre-v11 positional call cannot immediately append direct fallback output: that would create a newly ambiguous journal. The host fails closed before the fallback append. A fresh model-call retry is still allowed because it records a new BY_ID call first.

Compatibility

SQLite schema v11 is stamp-only:

  • no table or event is rewritten
  • old records preserve bytes, hashes, and positional behavior
  • old binaries reject v11 cleanly at Open instead of dropping the new hash-bound fields
  • operators must stop older writers/embedded clients and back up the database and WAL before first v11 open
  • rollback requires restoring the backup

Tests

Coverage includes:

  • provider failure + direct fallback + crash, with zero provider calls during Resume/Replay
  • exact, empty, wrong, and unknown correlation evidence in Replay and Resume
  • sticky divergence after a harness swallows the first error
  • direct output refusing correlated result evidence
  • trailing re-drive retaining the original call ID
  • same-run retry producing a distinct BY_ID call and result
  • pre-v11 positional replay
  • failed pre-v11 re-drive refusing immediate fallback while permitting a fresh correlated retry
  • optional-field wire presence and hash binding
  • Go/Python canonical interop vectors for correlated calls and results
  • v10-to-v11 stamp-only migration preserving raw event rows and schema

Verification

  • go test -race -shuffle=on ./controller ./wire ./canon ./sqlitelog
  • go test -race -shuffle=on ./session ./placement ./conformance ./harnesswire
  • go test -race -shuffle=on ./... in integrations/substrate
  • go test -race -shuffle=on ./... in integrations/claudecode
  • go build ./...
  • go vet ./...
  • buf lint
  • buf breaking --against ".git#tag=v0.4.0,subdir=api"
  • go run ./hack/doccheck
  • ./hack/verify_chain.sh
  • gofmt -l .
  • git diff --check

The repository-wide root test command still reaches the pre-existing Python 3.9 portability failure in internal/agentsessionsd.TestDocumentedPythonExecutorLogsStorageFailureWithoutPayload; all changed and dependent packages pass, including race-enabled tests.

@shashankbarsin

Copy link
Copy Markdown

I'm reviewing now

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Legacy recovery returns before flagging a trailing markerless model call as ambiguous, allowing fallback harness output to be replayed as a successful model result.

1 open issue
1 file not reviewed
  • api/genpb/common.pb.go: Generated file
What changed in this PR

This PR addresses #93 by linking durable model results to their call IDs, preventing direct harness fallback output from being replayed as provider success.

Changes:

  • Adds explicit result-correlation fields and shared matching rules for replay and recovery.
  • Preserves legacy positional matching and introduces a stamp-only SQLite schema v11 migration.
  • Adds regression tests, canonical hash vectors, and compatibility documentation.
File Description
wire/​convert.go Preserves correlation fields during conversion.
wire/​convert_test.go Tests correlation round-trips and hash stability.
sqlitelog/​sqlitelog.go Adds the stamp-only v11 migration.
sqlitelog/​model_correlation_schema_test.go Checks migration preserves schema and event rows.
sqlitelog/​cleanup_resources_test.go Updates migration expectations to the current schema.
spec.md Defines correlation, recovery, and upgrade rules.
hack/​verify_chain.py Adds Python correlation hash vectors.
docs/​session-data-model.md Explains correlated results and legacy matching.
docs/​harness-authoring.md Documents fallback and recovery behavior.
docs/​concepts.md Clarifies model-call and output event semantics.
docs/​api-reference.md Documents the new protobuf fields and enum.
controller/​resume.go Matches results by ID and guards ambiguous fallback.
controller/​resume_model_cut_test.go Tests correlation, fallback, divergence, and legacy recovery.
controller/​model_result.go Centralizes recorded-result matching.
controller/​legacy.go Counts successful durable sink writes.
controller/​executions.go Rejects correlation fields on non-output events.
controller/​effects.go Writes correlated results and validates replay matching.
changelog/​unreleased/​model-result-correlation.md Records the fallback replay fix.
changelog/​unreleased/​model-result-correlation-compat.md Records migration and compatibility requirements.
canon/​canon_test.go Tests canonical vectors and hash-bound field presence.
api/​types.go Adds Go correlation fields and mode constants.
api/​genpb/​common.pb.go Updates generated protobuf bindings.
api/​common.proto Defines optional result IDs and correlation modes.

Review effort: Balanced

Comment thread controller/resume.go Resolved

@aramase Anish Ramasekar (aramase) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The v11 correlation looks good for new calls; the legacy path still has the bug #93 describes (same as the open Copilot thread).

Nits:

  • changelog/unreleased/model-result-correlation.md:5-6: "no longer reinterpret direct fallback output" holds only for v11+ calls (EXE-10 says no retroactive repair); scope it to new calls.
  • docs/harness-authoring.md:285, spec.md:743, spec.md:790: the edited paragraphs leave short lines; rewrap each to 100 columns.

Comment thread controller/resume.go Resolved
Copilot AI balanced review requested due to automatic review settings November 29, 2026 18:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

A regression test is missing for markerless-call re-drive and replay, including ID and journal integrity checks and fail-closed cases, and the specification conflates model IDs with call IDs.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

Medium severity Test legacy-call re-drive and replay with ID validation

controller/​model_result.go:39

The compatibility tests do not cover a successful re-drive of a markerless call followed by replay of its new ID-bearing completion. Add a regression in controller/resume_model_cut_test.go that verifies the original call ID and journal prefix/hashes are preserved, then replays with zero provider calls. Include empty and wrong result IDs for this upgraded legacy shape to verify that it fails closed.

Low severity Distinguish model IDs from ModelCall.id call-correlation tokens

spec.md:730

The per-call IDs discussed in EXE-34/35 are model identifiers, but this sentence now places them in ModelCall.id, which is the independently generated call-correlation token. liveSink.model still records the selected model in ModelCall.model. Please distinguish model IDs from call IDs so the model-policy contract does not point consumers at the wrong field.

1 resolved since last review
1 file not reviewed
  • api/genpb/common.pb.go: Generated file

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 29, 2026 20:17
@Tatsinnit

Tatsat (Tats) Mishra 🐉 (Tatsinnit) commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Thank you for the careful follow-up, and apologies for the missed coverage. I addressed both points in 5f07f2b:

  • Added a markerless-call re-drive and replay regression that preserves the original call ID and full journal prefix/hashes, proves both regular and streaming provider callbacks remain unused on replay, and checks empty and wrong result IDs fail closed.
  • Corrected EXE-34/35 so selected model IDs remain in ModelCall.model, while ModelCall.id is clearly the independently generated call-correlation token.

The focused controller race suite, build, vet, doccheck, formatting, changelog, and cross-language chain checks pass. Thank you again for catching these gaps.

Anish Ramasekar (@aramase), when convenient, would you mind taking another look? GitHub would not let me formally re-request review from the fork. Oh, wait I can!! 🧙 = )

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed to evaluate the operational impact of compatibility guidance that omits the release introducing a one-way journal migration and stricter recovery behavior.

No open issues 🎉

1 file not reviewed
  • api/genpb/common.pb.go: Generated file

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Link model results to their MODEL_CALL by id instead of journal position

4 participants